package cn.cf.kxx;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.cf.kxx.adapter.GridValveAdapter;
import cn.cf.kxx.data.DataSharedPreferences;
import cn.cf.kxx.fram.OnActivityInteractionListener;
import cn.cf.kxx.fram.OnFragmentInteractionListener;
import cn.cf.kxx.fram.OnValveClickListener;
import cn.cf.kxx.model.Valve;
import cn.cf.kxx.service.IMyAidlCallBack;
import cn.cf.kxx.service.ISerialAidlInterface;
import cn.cf.kxx.service.SerialService;

public class ValveActivity extends Fragment implements View.OnClickListener , OnFragmentInteractionListener, OnValveClickListener {

    @Override
    public void onRefrashValues(Object obj) {
        refrashView();
    }
    @Override
    public void onRefrashString(String type) {
    }

    @Override
    public void onRefrashValve(int status) {

    }
    @Override
    public void click(Valve valve,String type) {
        //阀门1  在地址8
        switch (type){
            case "AOPEN":
                onActivityInteractionListener.sendCommand(5,valve.getFmBh()*2+6,1,new byte[]{1});
                break;
            case "ACLOSE":
                onActivityInteractionListener.sendCommand(5,valve.getFmBh()*2+6,1,new byte[]{0});
                break;
            case "BOPEN":
                onActivityInteractionListener.sendCommand(5,valve.getFmBh()*2+7,1,new byte[]{1});
                break;
            case "BCLOSE":
                onActivityInteractionListener.sendCommand(5,valve.getFmBh()*2+7,1,new byte[]{0});
                break;
        }
    }

    GridView gridView;
    Button goHome;
    GridValveAdapter gridViewAdapter;

    DataSharedPreferences sharedPreferences;
    List<Valve> valves = new ArrayList<>();

    View contentView;

    public static ValveActivity newInstance(Bundle args) {
        ValveActivity fragment = new ValveActivity();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = DataSharedPreferences.getInstance();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.activity_valve, container, false);
            initView();
        } else {
            ViewGroup parent = (ViewGroup) contentView.getParent();
            if (parent != null) {
                parent.removeView(contentView);
            }
        }
        return contentView;
    }

    private void initView() {
        gridView = contentView.findViewById(R.id.valve_list);
        goHome = contentView.findViewById(R.id.goHome);
        goHome.setOnClickListener(this);
        gridViewAdapter = new GridValveAdapter(getActivity());
        gridViewAdapter.setOnValveClickListener(this);
        gridViewAdapter.setmDataList(valves);
        gridView.setAdapter(gridViewAdapter);
        refrashView();
    }

    private void refrashView(){
        String content = sharedPreferences.getGuide(getActivity());
        Type listType = new TypeToken<ArrayList<Valve>>() {}.getType();
        valves = new Gson().fromJson(content, listType);
        gridViewAdapter.getmDataList().clear();
        gridViewAdapter.getmDataList().addAll(valves);
        gridViewAdapter.notifyDataSetChanged();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);

    }

    OnActivityInteractionListener onActivityInteractionListener;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        onActivityInteractionListener = (OnActivityInteractionListener) context;
    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.goHome){
            onActivityInteractionListener.bakeFragment();
        }
    }
}